package com.zdp.item.service;

import cn.hutool.Hutool;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.common.collect.Lists;
import com.zdp.common.domain.Items;
import com.zdp.common.domain.ItemsSpec;
import com.zdp.common.domain.rabbit.FreezeStockMessage;
import com.zdp.common.utils.JsonUtils;
import com.zdp.common.utils.RedisOperator;
import com.zdp.item.mapper.ItemsSpecMapper;
import com.zdp.item.mapper.ItemsSpecMapperCustom;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

/**
 * @author sesshomaru
 * @date 2021/7/10 14:36
 */
@Service
public class ItemSpecService {

    @Autowired
    private ItemsSpecMapper itemsSpecMapper;

    @Autowired
    private ItemsSpecMapperCustom itemsSpecMapperCustom;

    @Autowired
    private RedisOperator redis;

    // 根据商品id查询所有商品规格
    public List<ItemsSpec> findItemsSpecListByItemId(String itemId) {
        List<ItemsSpec> itemsSpecList = Lists.newArrayList();
        QueryWrapper<ItemsSpec> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("item_id", itemId);
        return itemsSpecMapper.selectList(queryWrapper);
    }

    @Transactional(rollbackFor = Exception.class)
    public void freezeStockFromRemain(FreezeStockMessage freezeStockMessage) {
        itemsSpecMapperCustom.freezeStockFromRemain(freezeStockMessage.getItemSpecId(), freezeStockMessage.getNumber());
    }

}
